package middleware

import (
	"day12_11_homework/pkg"
	"github.com/gin-gonic/gin"
)

func AutoMiddle() gin.HandlerFunc {
	return func(c *gin.Context) {
		header := c.GetHeader("token")
		if header == "" {
			c.JSON(500, gin.H{
				"message": "暂未登录",
			})
			return
		}
		token, s := pkg.GetToken(header)
		if s != "" {
			c.JSON(500, gin.H{
				"message": s,
			})
			return
		}
		s2 := token["user"].(string)
		c.Set("userId", s2)
		c.Next()
	}
}
